# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：handle_logs.py
#日期：2019-08-15
#备注：批量跑数据时，由于数据量大，需要对日志进行处理，找到ERROR信息,并定位具体问题
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

file_path = 'E:\zenglingwei\logs\\'

in_word = ['授信规则处理异常','第三方征信调用异常','规则引擎调用失败','封装同盾返回参数异常','接口出现异常']
out_word = ['发送授信结果通知返回业务失败','发送授信结果通知返回交易失败','进件请求报错','已有授信额度','该用户正在授信中','发送http-post请求异常','Servlet.service()']

exception = ['java.lang.NullPointerException: null','ProcessException']


# 拿到所有的报错信息
def get_errorInfo(file):
    i = 0
    with open(file, 'r', encoding='utf-8') as logs:
        for line in logs.readlines():
            i = i +1
            if 'ERROR' in line:
                line.lstrip()
                print('第' + str(i) + '行数据报错', line)


# 过滤掉不需要的信息
def get_errorDetail(file):
    i = 0
    with open(file, 'r', encoding='utf-8') as logs:
        for line in logs.readlines():
            i = i +1
            if ('ERROR' in line or '错误' in line) and (out_word[0] not in line and out_word[1] not in line and out_word[2] not in line and out_word[4] not in line and out_word[5] not in line and out_word[6] not in line):
                line.lstrip()
                print('第' + str(i) + '行数据报错', line)


# 获取需要的信息
def get_error(file):
    i = 0
    with open(file, 'r', encoding='utf-8') as logs:
        info = logs.readlines()
        for line in info:
            i = i +1
            for error in in_word:
                if 'ERROR' in line and error in line:
                    line.lstrip()
                    print('第' + str(i) +'行数据报错：' + line)
                    print('第' + str(i+1) + '行数据展示：' + info[i])
                    save_txt('第' + str(i) + '行数据展示：' + info[i-2])
                    save_txt('第' + str(i) +'行数据报错：' + line)
                    save_txt('第' + str(i+1) + '行数据报错：' + info[i])
                    save_txt('第' + str(i + 1) + '行数据展示：' + info[i+1] + '\n')


# 获取异常信息
def get_exception(file):
    i = 0
    with open(file, 'r', encoding='utf-8') as logs:
        info = logs.readlines()
        for line in info:
            i = i +1
            for error in exception:
                if error in line:
                    line.strip()
                    print('第' + str(i - 2) + '行数据', info[i - 3])
                    print('第' + str(i- 1) +'行数据',info[i-2])
                    print('第' + str(i) + '行数据报错', line)


# 保存报错信息
def save_txt(word):
    file = file_path + file_name + '.txt'
    with open(file, 'a',encoding= 'utf-8') as fp:
        fp.write(word)


# 拿到所有的报错信息
def get_logs(file):
    i = 0
    with open(file, 'r', encoding='utf-8') as logs:
        for line in logs.readlines():
            i = i +1
            if 'POR_' in line:
                line.lstrip()
                print('第' + str(i) + '行数据报错', line)
                save_txt(line)


if __name__ == '__main__':
    file_name = 'msydloan-web204-2019-09-27'
    file = file_path + file_name + '.log'
    #get_errorInfo(file)
    get_errorDetail(file)
    #get_error(file)
    #get_logs(file)
    #get_exception(file)

